<?php namespace Home\Controller; 

use Hdphp\Controller\Controller;

//前台默认控制器
class ListController extends Controller{
	
    //展示首页
    public function index(){
    	//获得顶级分类,
    	$cateModel =new \Common\Model\Cate;
		$topCate = $cateModel->orderBy('cid')->get();
		View::with('topCate',$topCate);
		// p($topCate);
        //获得商品分类cid
        $cid=Q('get.cid',0,'intval');
       	//处理商品分类
       	if($cid){
       		//获得商品分类名称
       		$cateName=$cateModel->where("cid={$cid}")->pluck('cname');
			//分配商品分类名称
			View::with('cateName',$cateName);
			//统计分类名称cid,(包括子分类)所有的商品总数
			$cids=$cateModel->getSon($cateModel->get(),$cid);
			//把自己商品分类的cid压入到所有商品分类的cid中
			$cids[]=$cid;
//			p($cids);
			$goodsModel=new \Common\Model\Goods;
			//统计每个分类下面共有多少件商品
			$total=$goodsModel->where("cate_cid in (".implode(',', $cids).")")->count();
//			p($total);
			//分配商品统计总数数
			View::with('total',$total);
			
			//获得分类表的所有数据
			$cateData=$cateModel->get();
			//获得分类表的所有pid
			$pids=$cateModel->getFather($cateData,$cid);
			//获得当前位置的数据,把数组里面的数据调换顺序
			$pids=array_reverse($pids);
			//获得当前位置的数据
			View::with('pids',$pids);
//			p(array_reverse($pids));
			
       	}
		//1根据当前的cid获得对应商品的gid
		$gids=$this->cidGetGids(Q('get.cid',0,'intval'));
//		p($gids);
		//2根据gid找到商品属性
		$goodsAttr=$this->gidGetAttr($gids);
		View::with('goodsAttr',$goodsAttr);
		//3处理筛选参数param 0_0_.....
		$num = count($goodsAttr);
		$param = isset($_GET['param']) ? explode('_', $_GET['param']) : array_fill(0, $num, 0);
		View::with('param',$param);
		//4.通过param参数筛选商品gid
		$resultGids = $this->filter($param,$gids);
//		p($resultGids);die;
		//根据顶级分类以及下面的子分类筛选出商品
		$goodsCate=$goodsModel->where("gid in (".implode(',', $resultGids).")")->get();
//			p($goodsCate);
		//分配商品数据
		View::with('goodsCate',$goodsCate);
        View::make();
    }
	//通过param参数筛选商品gid
	private function filter($param,$cidGids){
		$gids = array();
		foreach ($param as $gaid) {

			if($gaid){
				//1.找到gtvalue，比如找到"红色"
				$gtvalue = Db::table('goods_attr')->where("gaid={$gaid}")->pluck('gtvalue');
				//2.通过“红色”找到对应的商品id
				$gids[] = Db::table('goods_attr')->where("gtvalue='{$gtvalue}'")->lists('goods_gid');
			}
		}
		//如果点击了“不限”之外的属性
		if($gids){
//			$gids,exit;
			//取交集
			$gidsTemp = $gids[0];
//			p($gidsTemp);exit;
			foreach ($gids as $v) {
				$gidsTemp = array_intersect($gidsTemp, $v);
			}
			//和当前分类对应的gid再次取交集
			return array_intersect($gidsTemp, $cidGids);
		}else{//如果是全部是“不限”的时候
			return $cidGids;
		}
	}
	//通过gid获得商品的属性
	private function gidGetAttr($gids){
		$data=Db::table('goods_attr')->where("goods_gid in(" .implode(',', $gids) . ")")->groupBy('gtvalue')->get();
		//1把相同类型属性的id的归结在一起
		$sameAttr=array();
		foreach ($data as $v) {
			$sameAttr[$v['typeattr_taid']][]=$v;
		}
		//2组合数组
		$temp=array();
		foreach ($sameAttr as $typeattr_taid => $v){
			$temp[]=array(
				'name'=>Db::table('typeattr')->where("taid={$typeattr_taid}")->pluck('taname'),
				'value'=>$v
			);
		}
		return $temp;
	}
	//通过cid获得gid
	private function cidGetGids($cid){
		//1通过当前的cid获得所对应的所有子集cid
		$cids=$this->getSonCid(Db::table('cate')->get(),$cid);
		$cids[]=$cid;
		//2通过子集cid查找对应的商品的gid
		$gids = Db::table('goods')->where("cate_cid in(" . implode(',', $cids) . ")")->lists('gid');
		return $gids;
	}
	//递归获得指定cid的所有子集
	private function getSonCids($data,$cid){
		$temp=array();
		foreach ($data as $v) {
			if($v['pid']==$cid){
				$temp[]=$v['cid'];
				$temp=array_merge($temp,$this->getSonCids($data,$v['cid']));
			}
		}
		return $temp;
	}

}
